<?php
class RM_Admin_ReviewsController extends RM_Controller
{
    /**
     * This returns the JSON for the reviews list.
     */
    public function reviewslistJsonAction(){

        $offset = $this->_getParam('start');
        $count = $this->_getParam('limit');
        $sort = $this->_getParam('sort', 'id');
        $direction = $this->_getParam('dir', 'DESC');
        $filters = $this->_getParam('filter', array());
        $order = $sort . ' ' . $direction;

        $iso = $this->_getParam('iso', RM_Environment::getInstance()->getLocale());

        $dao = new RM_Reviews();
        //$unitsdao = new RM_ReviewsUnits();

        $total = $dao->getAll()->count();
        $rows = $dao->getAll();

        foreach ($rows as $row){
            $jsonArray[] = array("id"=>$row->id,"score"=>$row->score,"en_GB"=>$row->en_GB,"fr_FR"=>$row->fr_FR);
        }

        $json = new stdClass;
        $json->total = $total;
        $json->data = $jsonArray;

        return array(
            'data' => $json
        );
    }

    /**
     * delete from list
     */
    public function reviewsdeleteJsonAction(){
        $ids = $this->_getParam('ids', array());
        $model = new RM_Reviews();

        foreach ($ids as $id) {
            $row = $model->find($id)->current();
            if ($row === null) continue;
            $row->delete();
        }

        return array(
            'data' => array('success' => true)
        );
    }

    /**
     * load an amenity detail...
     */
    public function loadJsonAction(){

        $id = $this->_getParam('id');
        $language = $this->_getParam('language');

        $json = new stdClass;
        if ($id) {
            $dao = new RM_Reviews();
            $unitsdao = new RM_ReviewsUnits();
            
            $data = $dao->getReviewsDetails($id)->current()->toArray();
            $total = $dao->getReviewsDetails($id)->count();

            $unitsArray = $unitsdao->getAll("amenity_id='".$data['id']."'")->current();
            $data['units'] = implode(",", $data['units']);

            $json->totalUnits = $total;
            $json->data = $data;
        }

        return array(
            'data' => $json
        );
    }

    /**
     * update the reviews
     */
    public function updateJsonAction(){

        $id = $this->_getParam('id');
        $iso = $this->_getParam('language');
        $name = $this->_getParam('name');
        $info_url = $this->_getParam('info_url');
        $icon = $this->_getParam('icon');
        $units = $this->_getParam('units');

        $model = new RM_Reviews();
        $unitsModel = new RM_ReviewsUnits();

        // save the info
        if ($id){
            // update if ID present
            $model->update(array("info_url"=>$info_url,"icon_url"=>$icon,$iso=>$name),"id='".$id."'");
        } else {
            // insert
            $id = $model->insert(array("info_url"=>$info_url,"icon_url"=>$icon,$iso=>$name));
        }

        // save the units
        if ($units !== "false"){
            $unitsArray = explode(",",$units);
            foreach ($unitsArray as $unit_id){
                if ($unit_id) {
                    $unitsModel->delete("unit_id='".$unit_id."'");
                    $unitsModel->insert(array("unit_id"=>$unit_id, "amenity_id"=>$id));
                }
            }
        }

        return array(
            'data' => array('success' => true)
        );
    }

    /**
     * image upload
     *
     * @return JSON
     */
    public function imguploadJsonAction(){

        $module = new RM_Module_Reviews();

        $manager = new RM_Media_Extensions_Manager($module->name);
        try {
            $filename = $manager->upload('extension_media_upload', 'tiny');
            return array('data' => array('success' => true, "filename"=>$filename));
        } catch (RM_Exception $exception) {
            return array('data' => array('success' => false, 'error' => $exception->getMessage()));
        }
    }

    /**
     * Get the reviews config
     *
     * @return JSON
     */
    public function configJsonAction(){
        $model = new RM_ReviewsConfig();
        $settings = $model->fetchAll("id='1'")->current();

        return array(
            'data' => array("show_score" => $settings->id)
        );
    }

    /**
     * Save the reviews config
     *
     * @return JSON
     */
    public function updateconfigJsonAction(){

        $show_score = $this->_getParam('rm_pages_modules_reviews_show_show');

        $model = new RM_ReviewsConfig();
        $model->update(array("show_score"=>$show_score),"id='1'");
        return array(
            'data' => array('success' => true)
        );
    }

    /**
     * This returns all reviews and the unit selection
     *
     * @return JSON
     */
    public function getreviewsJsonAction(){

        $unit_id = $this->_getParam('unit_id');
        $iso = $this->_getParam('iso', RM_Environment::getInstance()->getLocale());

        $model = new RM_Reviews();
        
        $selectedArray = $model->getbyUnit($unit_id, $iso);
        foreach ($selectedArray as $selected){
            $selectedIDs[] = $selected['id'];
        }

        $data = $model->getAll()->toArray();

        foreach($data as $item){
            $newData[] = array("id"=>$item[id],"name"=>$item[RM_Environment::getInstance()->getLocale()],"icon_url"=>$item['icon_url']);
        }

        return array(
            'data' => array("data" => $newData, 'selection' => $selectedIDs )
        );
    }

    public function updateunitJsonAction(){
        $unit_id = $this->_getParam('rm_reviews_edit_unit_id');
        $selectedReviews = $this->_getParam('rm_pages_reviews_edit_unit_selection_checkbox');

        $unitsModel = new RM_ReviewsUnits();
        $unitsModel->delete("unit_id='".$unit_id."'");
                
        // save the units
        foreach ($selectedReviews as $key=>$value){
            if ($value == "1") {
                $unitsModel->insert(array("unit_id"=>$unit_id, "amenity_id"=>$key));
            }
        }

        return array(
            'data' => array('success' => true)
        );
        
    }
}